#include <iostream>
#include <fstream>
#include <map>
#include <stack>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
using namespace std;
const int N = 550;
bool VIS[N];
int PLCAE_NUM=0;
string LOCATION_INFORMATION[N][N];
int VOTES_SUM[N];
string BEGIN_NAME,END_NAME;


//定义结构体存储路线信息
struct Edge
{
    int startTimeHour,endTimeHour,startTimeMin,endTimeMin;
    int time;  //两地之间所需时间
    double softSleeperFare,hardSleeperFare,hardSeatFare;//软卧，硬卧，软座票价
    int v;//两地之间有路

} mp[101][101];

struct Register
{
    string name,power;
} R;

map<string,int> VIS1; //地点对应数字
map<int,string> VIS2; //数字对应地点
map<string,int>REGISTERSIZE;//账户对应数字
map<string,string>NAMES;
stack <int> S;
vector<int> ROUTE;
vector<vector<int> > ROUTES;


void Init(); //初始化
void Information();
void Initfile();
int findName(string a);
int timeDifference(struct Edge b,struct Edge a);
int conversionTime(int sTimeHour,int sTimeMin,int eTimeHour,int eTimeMin);//计算时间差
void printLine(int num);
void timeshow(int start,int end1);//输出每段路程的起始到达时间
void edgeInit();
void add();
void meau();
void deleteRoute();
void modifyRoute(); //修改路线
void Administration();
void registerAndLogIn();
void selectRoute();
int land();
void  informationService();//信息查询
void hi();
void ticketingSystem();
void Route(int begin1,int end1);//求起点和终点间全部路径
void leastTransfer(int beginl,int end1 );//求换乘最少
void selectLine(int num,int x);//选择路线
void minimalLikelihood();//错过换成可能性最小
void leastFare();//票价最少
void lastestTime();//求耗时最少的路
void Init() //初始化
{
    fstream f;
    f.open("placeData.txt",ios::out|ios::app);
    f.close();
    f.open("placeData.txt",ios::in);
    string place;

    while(f>>place)
    {
        VIS1[place]=PLCAE_NUM;
        VIS2[PLCAE_NUM]=place;
        PLCAE_NUM++;
        //cout<<place;
    }//给地点用对应的数字赋值
    f.close();

}

void Information()
{
    fstream f;
    f.open("ticketInformation.txt",ios::out|ios::app|ios::in);
    f.close();
}

void Initfile()
{
    fstream f;
    f.open("register.txt",ios::out|ios::app|ios::in);
    f.close();
}

int findName(string a)
{
    fstream f;
    f.open("placeData.txt",ios::in);
    string place;

    while(f>>place)
    {
        if(a==place)
        {
            return 1;
        }
        //cout<<place;
    }//给地点用对应的数字赋值
    f.close();
    return 0;
}

int conversionTime(int sTimeHour,int sTimeMin,int eTimeHour,int eTimeMin)//计算时间差
{
    if(sTimeHour>eTimeHour)
    {
        eTimeHour=eTimeHour+24;
    }
    else if(eTimeHour==sTimeHour)
    {
        if(sTimeMin>eTimeMin)
        {
            eTimeHour=eTimeHour+24;
        }
    }
    return (eTimeHour-sTimeHour)*60+(eTimeMin-sTimeMin);
}


int timeDifference(struct Edge b,struct Edge a)
{
    return conversionTime(a.endTimeHour,a.endTimeMin,b.startTimeHour,b.startTimeMin);
}

void timeshow(int start,int end1)//输出每段路程的起始到达时间
{
    cout<<VIS2[start];
    cout<<"->";
    cout<<VIS2[end1];
    cout<<' ';
    cout<<"出发时间:";
    cout<<mp[start][end1].startTimeHour<<':';
    if(mp[start][end1].startTimeMin<10)
    {
        cout<<0<<mp[start][end1].startTimeMin<<" ";
    }
    else
        cout<<mp[start][end1].startTimeMin<<" ";
    cout<<"到达时间:";
    cout<<mp[start][end1].endTimeHour<<':';
    if(mp[start][end1].endTimeMin<10)
    {
        cout<<0<<mp[start][end1].endTimeMin<<" ";
    }
    else
        cout<<mp[start][end1].endTimeMin<<" ";
    cout << endl;
}



void printLine(int num)
{
    for(int j  = ROUTES[num].size()-1; j>=0; j--)
    {

        if(j==0)
            cout << VIS2[ROUTES[num][j]];
        else cout << VIS2[ROUTES[num][j]]<<"->";

    }
    cout <<endl;
    double softPrice_all = 0.0,hardPrice_all = 0.0,hardSeatPrice_all = 0.0;
    for(int j  = ROUTES[num].size()-1; j>=1; j--)
    {
        hardPrice_all+=mp[ROUTES[num][j]][ROUTES[num][j-1]].hardSleeperFare;
        softPrice_all+=mp[ROUTES[num][j]][ROUTES[num][j-1]].softSleeperFare;
        hardSeatPrice_all+=mp[ROUTES[num][j]][ROUTES[num][j-1]].hardSeatFare;
        timeshow(ROUTES[num][j],ROUTES[num][j-1]);
        cout << "车厢:"<<"硬卧"<<" "<<"票价:"<<mp[ROUTES[num][j]][ROUTES[num][j-1]].hardSleeperFare<<" "<<"车厢:"<<"软卧"<<" "<<"票价:"<<mp[ROUTES[num][j]][ROUTES[num][j-1]].softSleeperFare<<" "<<"车厢:"<<"硬卧"<<" "<<"票价:"<<mp[ROUTES[num][j]][ROUTES[num][j-1]].hardSeatFare;
        cout<<endl;

    }
    printf("总票价:硬卧:%.2f 软卧;%.2f 硬座:%.2f\n",hardPrice_all,softPrice_all,hardSeatPrice_all);
}




void edgeInit()
{
    fstream f;
    f.open("edgeData.txt",ios::out|ios::app);
    f.close();
    f.open("edgeData.txt",ios::in);
    string startingPoint,End;
    int startTimeHour,endTimeHour,startTimeMin,endTimeMin;
    double softSleeperFare,hardSleeperFare,hardSeatFare;
    char s,t;
    while(f>>startingPoint)
    {
        f>>End>>startTimeHour>>s>>startTimeMin>>endTimeHour>>t>>endTimeMin>>softSleeperFare>>hardSleeperFare>>hardSeatFare;
        mp[VIS1[startingPoint]][VIS1[End]].v=1;
        mp[VIS1[startingPoint]][VIS1[End]].softSleeperFare=softSleeperFare;
        mp[VIS1[startingPoint]][VIS1[End]].hardSleeperFare=hardSleeperFare;
        mp[VIS1[startingPoint]][VIS1[End]].hardSeatFare=hardSeatFare;
        mp[VIS1[startingPoint]][VIS1[End]].startTimeHour = startTimeHour;
        mp[VIS1[startingPoint]][VIS1[End]].startTimeMin = startTimeMin;
        mp[VIS1[startingPoint]][VIS1[End]].endTimeHour = endTimeHour;
        mp[VIS1[startingPoint]][VIS1[End]].endTimeMin = endTimeMin;
        mp[VIS1[startingPoint]][VIS1[End]].time = conversionTime(startTimeHour,startTimeMin,endTimeHour,endTimeMin);

    }
    f.close();
}

void meau()
{
    printf("\n");
    printf("\t\t\t\t☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
    printf("\t\t\t\t☆==============欢迎使用车票系统==================☆\n");
    printf("\t\t\t\t☆~~~~~~~~~~~~~~~~~1.登陆~~~~~~~~~~~~~~~~~~~~~~~~~☆\n");
    printf("\t\t\t\t☆~~~~~~~~~~~~~~~~~2.注册并登录~~~~~~~~~~~~~~~~~~~☆\n");
    printf("\t\t\t\t☆~~~~~~~~~~~~~~~~~3.订票信息查询~~~~~~~~~~~~~~~~~☆\n");
    printf("\t\t\t\t☆~~~~~~~~~~~~~~~~~0.退出系统~~~~~~~~~~~~~~~~~~~~~☆\n");
    printf("\t\t\t\t☆================================================☆\n");
    printf("\t\t\t\t☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
    printf("========================================================================================================================\n");
    printf("请选择编号（0-3）选择功能进行相应的操作：");
}

void add()
{
    int startTimeHour,endTimeHour,startTimeMin,endTimeMin;
    double softSleeperFare,hardSleeperFare,hardSeatFare;
    string BEGIN_NAME,END_NAME;
    char s,t;
    while(1)
    {
        int flag = 0;
        fstream f1,f2;
        f1.open("placeData.txt",ios::out|ios::app|ios::in);
        f2.open("edgeData.txt",ios::out|ios::app|ios::in);
        cout << "请输入增加的站点信息:\n";
        cout <<"起点:";
        cin >> BEGIN_NAME ;
        cout <<"终点:";
        cin>> END_NAME;
        cout<< "出发时间:";
        cin  >> startTimeHour >> s >> startTimeMin ;
        cout <<"到站时间:";
        cin >> endTimeHour >>t >> endTimeMin ;
        cout <<"软卧票价:";
        cin>> softSleeperFare ;
        cout << "硬卧票价:";
        cin >> hardSeatFare ;
        cout << "硬座票价:";
        cin  >> hardSeatFare;
        if(!findName(BEGIN_NAME))
        {
            flag = 1;
            f1 << BEGIN_NAME<<endl;
            VIS1[BEGIN_NAME]=PLCAE_NUM;
            VIS2[PLCAE_NUM]=BEGIN_NAME;
            PLCAE_NUM++;
        }
        if(!findName(END_NAME))
        {
            flag = 1;
            f1<< END_NAME<<endl;
            VIS1[END_NAME]=PLCAE_NUM;
            VIS2[PLCAE_NUM]=END_NAME;
            PLCAE_NUM++;
        }
        f1.close();

        string a,b,c,d,e,f,g;
        struct ab {
            string a;
            string b;
        } abs[N];
        int i = 0;
        while(f2 >> a >> b >>c>>d>>e>>f>>g)
        {
            abs[i].a = a;
            abs[i].b = b;
            i++;
            //NAMES[a] = b;
        }
        f2.close();
        f2.open("edgeData.txt",ios::out|ios::app|ios::in);
        bool fl = false;
        for(int j = 0;j<i;j++)
        {
         if(abs[j].a == BEGIN_NAME&&abs[j].b == END_NAME)
        {
        fl = true;
        cout << "地点和终点之间已经存在直达的线路:\n";
        break;
    }


}
if (!fl) {
    flag = 1;
} else {
    flag = 0;
}        if(flag == 1)
        {
            mp[VIS1[BEGIN_NAME]][VIS1[END_NAME]].v=1;
            mp[VIS1[BEGIN_NAME]][VIS1[END_NAME]].softSleeperFare=softSleeperFare;
            mp[VIS1[BEGIN_NAME]][VIS1[END_NAME]].hardSleeperFare=hardSleeperFare;
            mp[VIS1[BEGIN_NAME]][VIS1[END_NAME]].hardSeatFare=hardSeatFare;
            mp[VIS1[BEGIN_NAME]][VIS1[END_NAME]].startTimeHour = startTimeHour;
            mp[VIS1[BEGIN_NAME]][VIS1[END_NAME]].startTimeMin = startTimeMin;
            mp[VIS1[BEGIN_NAME]][VIS1[END_NAME]].endTimeHour = endTimeHour;
            mp[VIS1[BEGIN_NAME]][VIS1[END_NAME]].endTimeMin = endTimeMin;
            mp[VIS1[BEGIN_NAME]][VIS1[END_NAME]].time = conversionTime(startTimeHour,startTimeMin,endTimeHour,endTimeMin);

            f2 << BEGIN_NAME <<" "<< END_NAME<<" "<< startTimeHour <<s << startTimeMin <<" " <<endTimeHour << t << endTimeMin<<" " << softSleeperFare<<" "<< hardSleeperFare<<" " << hardSeatFare<<endl;
            cout << "站点信息添加完成，是否选择继续添加:继续添加（1）,退出（0）:\n";
            int j ;
            cin >> j;
            if(j == 1)
                continue;
            else return;

        }
        f2.close();
    }


}

void deleteRoute()
{
    int f;
    while(1)
    {
        //system("cls");
        string sName,eName;
        cout<<"请输入待删除路线的起点:";
        cin>>sName;
        cout<<"请输入待删除路线的终点:";
        cin>>eName;
        if(mp[VIS1[sName]][VIS1[eName]].v==1)
        {
            cout<<"请确定是否是以下线路\n";
            timeshow(VIS1[sName],VIS1[eName]);
            cout<<"请输入0表示确定，1表示返回:\n";
            cin>>f;
            if(f==0)
            {
                fstream f;
                f.open("edgeData.txt",ios::out);
                mp[VIS1[sName]][VIS1[eName]].v=0;
                for(int i=0; i<PLCAE_NUM; i++)
                {
                    for(int j=0; j<PLCAE_NUM; j++)
                    {
                        if(mp[i][j].v==1)
                        {
                            f<<VIS2[i]<<' '<<VIS2[j]<<" "<<mp[i][j].startTimeHour<<":"<<mp[i][j].startTimeMin<<" ";
                            f<<mp[i][j].endTimeHour<<":"<<mp[i][j].endTimeMin<<' '<<mp[i][j].softSleeperFare<<' ';
                            f<<mp[i][j].hardSleeperFare<<' '<<mp[i][j].hardSeatFare<<endl;
                        }
                    }
                }
                cout << "删除成功，按任意键返回:";
                system("pause");
                //system("cls");
                break;

            }
            else if(f==1)
            {
                continue;
            }
        }
    }
}

void modifyRoute() //修改路线
{
    int f;
    system("cls");
    while(1)
    {

        string sName,eName;
        cout<<"请输入待修改路线的起点:";
        cin>>sName;
        cout<<"请输入待修改路线的终点:";
        cin>>eName;
        if(mp[VIS1[sName]][VIS1[eName]].v==1)
        {
            cout<<"请确定是否是以下线路\n";
            timeshow(VIS1[sName],VIS1[eName]);
            cout<<"软卧票价:"<<mp[VIS1[sName]][VIS1[eName]].softSleeperFare<<' ';
            cout<<"硬卧票价:"<<mp[VIS1[sName]][VIS1[eName]].hardSleeperFare<<' ';
            cout<<"硬座票价:"<<mp[VIS1[sName]][VIS1[eName]].hardSeatFare<<endl;
            cout<<"请输入0表示确定，1表示返回\n";
            cin>>f;
            if(f==0)
            {
                while(1)
                {
                    cout<<"选择修改目标，1.发车时间，2.到达时间，3.软卧票价，4.硬卧票价，5.硬座票价。";
                    int j;
                    cin>>j;
                    if(j==1)
                    {
                        cout<<"请输入新发车时间:";
                        int newTimeHour,newTimeMin;
                        char s;
                        cin>>newTimeHour>>s>>newTimeMin;
                        mp[VIS1[sName]][VIS1[eName]].startTimeHour=newTimeHour;
                        mp[VIS1[sName]][VIS1[eName]].startTimeMin=newTimeMin;
                        cout<<"请输入0表示确认或输入1返回\n";
                        timeshow(VIS1[sName],VIS1[eName]);
                        int k;
                        cin>>k;
                        if(k==0)
                        {
                            break;
                        }
                        else
                        {
                            system("cls");
                            continue;
                        }
                    }
                    else if(j==2)
                    {
                        cout<<"请输入新到达时间:";
                        int newTimeHour,newTimeMin;
                        char s;
                        cin>>newTimeHour>>s>>newTimeMin;
                        mp[VIS1[sName]][VIS1[eName]].endTimeHour=newTimeHour;
                        mp[VIS1[sName]][VIS1[eName]].endTimeMin=newTimeMin;
                        cout<<"请输入0表示确认或输入1返回\n";
                        timeshow(VIS1[sName],VIS1[eName]);
                        int k;
                        cin>>k;
                        if(k==0)
                        {
                            break;
                        }
                        else
                        {
                            system("cls");
                            continue;
                        }
                    }
                    else if(j==3)
                    {
                        cout<<"请输入新软卧票价:";
                        double s;
                        cin>>s;
                        mp[VIS1[sName]][VIS1[eName]].softSleeperFare=s;
                        cout<<"请输入0表示确认或输入1返回:\n";
                        timeshow(VIS1[sName],VIS1[eName]);
                        int k;
                        cin>>k;
                        if(k==0)
                        {
                            break;
                        }
                        else
                        {
                            system("cls");
                            continue;
                        }
                    }
                    else if(j==4)
                    {
                        cout<<"请输入新硬卧票价:";
                        double s;
                        cin>>s;
                        mp[VIS1[sName]][VIS1[eName]].hardSleeperFare=s;
                        cout<<"请输入0表示确认或输入1返回\n";
                        timeshow(VIS1[sName],VIS1[eName]);
                        int k;
                        cin>>k;
                        if(k==0)
                        {
                            break;
                        }
                        else
                        {
                            system("cls");
                            continue;
                        }
                    }
                    else if(j==5)
                    {
                        cout<<"请输入新硬座票价:";
                        double s;
                        cin>>s;
                        mp[VIS1[sName]][VIS1[eName]].hardSeatFare=s;
                        cout<<"请输入0表示确认或输入1返回\n";
                        timeshow(VIS1[sName],VIS1[eName]);
                        int k;
                        cin>>k;
                        if(k==0)
                        {
                            break;
                        }
                        else
                        {
                            system("cls");
                            continue;
                        }
                    }
                }
                fstream f;
                f.open("edgeData.txt",ios::out);
                for(int i=0; i<PLCAE_NUM; i++)
                {
                    for(int j=0; j<PLCAE_NUM; j++)
                    {
                        if(mp[i][j].v==1)
                        {
                            f<<VIS2[i]<<' '<<VIS2[j]<<" "<<mp[i][j].startTimeHour<<":"<<mp[i][j].startTimeMin<<" ";
                            f<<<<mp[i][j].endTimeHour<<":"<<mp[i][j].endTimeMin<<' '<<mp[i][j].softSleeperFare<<' ';
                            f<<mp[i][j].hardSleeperFare<<' '<<mp[i][j].hardSeatFare<<endl;
                        }
                    }
                }
                break;

            }
            else if(f==1)
            {
                continue;
            }
        }
        else
        {
            cout<<"没有找到线路，请检查一下输入站点"<<endl;

        }
    }
    system("cls");
    cout<<"修改成功";
    system("pause");
    system("cls");
}


void Administration()
{
    while(1)
    {
        system("cls");
        printf("\n\t\t\t\t------------登陆成功-------------\n");
        printf("\t\t\t\t--------欢迎进入管理系统---------\n");
        cout << "\t\t\t\t-----------路线管理(1)-----------\n";
        cout << "\t\t\t\t-----------退出(0)---------------\n";
        int i;
        cin >> i;
        switch (i)
        {

        case 1 :
        {
            system("cls");
            cout << "\t\t\t\t--------增加线路(1)--------\n";
            cout <<"\t\t\t\t--------删除线路(2)--------\n";
            cout <<"\t\t\t\t--------修改线路(3)--------\n";
            int j;
            cin >>j;
            switch(j)
            {
            case 1:
            {
                system("cls");
                add();
                break;
            }
            case 2:
            {
                system("cls");
                deleteRoute();
                break;
            }
            case 3:
            {
                system("cls");
                modifyRoute();
                break;
            }
            }
            break;
        }
        case 0:
        {
            return ;
        }
        }
    }

}

void registerAndLogIn()
{


    int flag = 0;
    int i = 0;
    fstream f,f1;
    f.open("register.txt",ios::out|ios::app|ios::in);
    string a,b;
    while(f >> a >> b)
    {
        REGISTERSIZE[a] = ++i;
    }
    f.close();
    cout<<"\t\t\t\t\t-------------注册-------------"<<endl;
    while(1)
    {


        f1.open("register.txt",ios::out|ios::app|ios::in);
        cout << "用户名:";
        cin >> R.name;
        if(R.name == "pjgly")
        {
            cout << "此用户名已被注册，请重新注册\n";
            continue;
        }
        if(i == 0)
        {
            cout << "密码:";
            cin >> R.power;
            f1 << R.name << " "<< R.power<<endl;
            return;
        }
        else if(i!=0)
        {
            //cout <<registersize.size()<<" ";

            for(int j = 1; j<=REGISTERSIZE.size(); j++)
            {
//cout <<registersize[r.name]<<" ";
                if(REGISTERSIZE[R.name] == j)
                {

                    flag = 1;
                    break;
                }
                //cout<<flag<<endl;
            }
            if(flag == 0)
            {
                cout << "密码：";
                cin >> R.power;
                f1 <<R.name<< " "<< R.power<<endl;
                return;
            }
            else
            {
                system("cls");
                cout << "用户名已存在，请重新输入\n";
                flag = 0;
            }

        }

        f1.close();
    }


}


int land()
{
    int  i=0;      //i是为了判断密码长度 并加密
    char a[10];
    char b[10];


    printf("请输入您的用户名：");
    cin>>R.name;

    printf("请输入您的密码：");

    cin>>R.power;
    getchar();


    if(R.name == "pjgly"&&R.power == "666666")
    {
        system("cls");
        Administration();
    }

    else
    {
        FILE *fp;

        fp=fopen("register.txt","rt");       //又打开文件

        if(fp==NULL)
        {
            printf("文件不存在!");
            exit(1);
        }

        while(fscanf(fp,"%s %s",a,b)!=EOF)    //读文件判断账号密码是否正确
        {
            if(a==R.name&&  b==R.power)   //字符串比较函数，看是否正确
            {
                system("cls");
                cout << "正在登陆中";
                cout << '.';
                Sleep(1000);
                cout << '.';
                Sleep(1000);
                cout << '.';
                Sleep(1000);
                system("cls");
                ticketingSystem();
                return 0;
            }
        }
        if(1)
        {
            printf("信息输入错误！\n");
            while(1)
            {
                cout <<"请注册新用户（1）或者重新登录（0）;\n";
                string j;
                cin >> j;
                if(j == "0")
                {
                    system("cls");
                    land();
                    return 0;
                }

                else if(j == "1")
                {
                    system("cls");
                    registerAndLogIn();
                    system("cls");
                    cout << "正在登陆中";
                    cout << '.';
                    Sleep(1000);
                    cout << '.';
                    Sleep(1000);
                    cout << '.';
                    Sleep(1000);
                    system("cls");
                    ticketingSystem();
                    return 0;

                }
                else
                {
                    system("cls");
                    cout <<"请输入正确的指令";
                    cout << '.';
                    Sleep(1000);
                    cout << '.';
                    Sleep(1000);
                    cout << '.';
                    Sleep(1000);
                    system("cls");

                }

            }


        }
        fclose(fp);

    }
}

void  informationService()//信息查询
{

    while(1)
    {
        int flag = 0;
        int i = 0;
        fstream f,f1;
        string a,b;
        int c;
        double d;

        cout <<"请输入用户名:";
        cin >>R.name;
        cout <<"请输入密码:";
        cin >> R.power;

        int v=0;//标记密码是否正确

        f.open("register.txt",ios::out|ios::app|ios::in);

        while(f >> a >> b)
        {
            if(a==R.name && b==R.power)
            {
                REGISTERSIZE[a] = ++i;
            }

        }
        f.close();
        if(i == 0)
        {
            cout <<"不存在该用户或密码错误,请重新输入(0)或选择注册(1):\n";
            int j ;
            cin >> j;
            if(j == 0)
            {

                system("cls");
            }
            else if(j ==1)
            {
                system("cls");
                registerAndLogIn();
                system("cls");
                cout << "正在登陆中";
                cout << '.';
                Sleep(1000);
                cout << '.';
                Sleep(1000);
                cout << '.';
                Sleep(1000);
                system("cls");
                ticketingSystem();
                return;

            }

        }

        else if(i!=0)
        {
            f.open("ticketInformation.txt",ios::out|ios::app|ios::in);
            int sum1 = 0;
            string k,linedata;
            int linesum;
            while(f >> k >> linesum)
            {
                if(k == R.name)
                {
                    sum1++;
                    flag=1;
                }
                for(int j=0; j<linesum; j++)
                {
                    f >> linedata;
                }
            }
            f.close();

            if(flag == 1)
            {

                int x = 0,flag1 = 0;
                fstream f;
                f.open("ticketInformation.txt",ios::out|ios::app|ios::in);
                if(x<sum1)
                {
                    while(f >> a )
                    {
                        //cout<<a;

                        if(a ==R.name)
                        {
                            x++;
                            flag1 = 1;
                            while(f >>c)
                            {

                                VOTES_SUM[x] = c;
                                //cout<<c<<endl;
                                for(int m = 1; m<=c; m++)
                                {
                                    f >> LOCATION_INFORMATION[x][m];

                                }
                                break;
                            }

                        }

                    }
                }
                f.close();

                system("cls");
                cout <<"正在查询中请稍等";
                Sleep(500);
                cout << '.';
                Sleep(500);
                cout << '.';
                Sleep(500);
                cout << '.';
                Sleep(500);
                system("cls");
                //cout<<VOTES_SUM1;
                for(int i = 1; i<=sum1; i++)
                {
                    //cout<<VOTES_SUM[i]<<endl;
                    for(int j = 1; j<=VOTES_SUM[i]; j++)
                    {
                        if(j == VOTES_SUM[i])
                            cout << LOCATION_INFORMATION[i][j];
                        else cout << LOCATION_INFORMATION[i][j]<< "->";
                    }
                    cout << endl;
                    for(int k = 1; k<VOTES_SUM[i]; k++)
                    {
                        //cout << "1";
                        timeshow(VIS1[LOCATION_INFORMATION[i][k]],VIS1[LOCATION_INFORMATION[i][k+1]] ); //cout << endl;
                    }
                }
                cout << endl;

                cout<<"查询完成，请按任意键返回";
                system("pause");
                return;
            }
            else
            {

                cout <<"暂无购票信息,请购票\n";
                cout<<"正在跳转";
                Sleep(1000);
                cout << '.';
                Sleep(1000);
                cout << '.';
                Sleep(1000);
                cout << '.';
                Sleep(1000);
                system("cls");
                ticketingSystem();
                return;

            }

        }

    }


}




void hi()
{
    string a;
    cin >> a;
    if(a=="0")
    {
        exit(1);
    }
    else if(a=="1")
    {
        //管理+
        system("cls");
        land();
    }
    else if(a=="2")
    {
        system("cls");
        registerAndLogIn();
        system("cls");
        cout << "正在登陆中";
        cout << '.';
        Sleep(1000);
        cout << '.';
        Sleep(1000);
        cout << '.';
        Sleep(1000);
        system("cls");
        ticketingSystem();

    }
    else if(a == "3")
    {
        system("cls");
        informationService();
    }
}

void Route(int begin1,int end1)//求起点和终点间全部路径
{

    S.push(begin1);
    VIS[begin1] = 1;
    int top_elempet;//记录栈顶元素
    int tmp;
    int c = -1;//标记出栈之后的栈顶
    while(S.size()>0){
        top_elempet = S.top();
        if(top_elempet == end1){
            while(!S.empty())//将经过的点存进route容器
            {
                tmp = S.top();
                S.pop();
                ROUTE.push_back(tmp);
            }
            ROUTES.push_back(ROUTE);//将完整路径存入ROUTES;
            for(vector<int>::reverse_iterator rit = ROUTE.rbegin(); rit != ROUTE.rend(); rit++)
            {
                S.push(*rit);
            }
            ROUTE.clear();//清空单挑路径
            c = S.top();
            S.pop();
            VIS[top_elempet] = 0;
            top_elempet = S.top();
        }
        else
        {
            int i ;
            for(i = c+1; i<PLCAE_NUM; i++) {
                if(!VIS[i]&&mp[top_elempet][i].v ==1){
                    VIS[i] = 1;
                    S.push(i);
                    c = -1;//一条路径搜索完后接着重新搜索
                    break;
                }
            }

           if(i == PLCAE_NUM){
                top_elempet = S.top();
                VIS[top_elempet] = 0;
                c = S.top();
                S.pop();
            }
        }
    }


}


void leastTransfer(int beginl,int end1 )//求换乘最少

{
    int minimum = 0x3f3f3f3f,temp;
    for(int i = 0; i<ROUTES.size(); i++)
    {
        //cout << i<<endl;
        if(minimum>ROUTES[i].size()-1)
        {
            minimum = ROUTES[i].size() -1;
            temp = i;
        }
    }
    cout<<"花费时间最短的为第"<<temp+1<<"条"<<endl;
    for(int j  = ROUTES[temp].size()-1; j>=0; j--)
    {

        if(j==0)
            cout << VIS2[ROUTES[temp][j]];
        else cout << VIS2[ROUTES[temp][j]]<<"->";

    }
    cout <<endl;
    for(int j  = ROUTES[temp].size()-1; j>=1; j--)
    {

        timeshow(ROUTES[temp][j],ROUTES[temp][j-1]);
        cout<<endl;

    }

}

void lastestTime()//求耗时最少的路
{
    int VOTES_SUM[ROUTES.size()]= {0};
    int min1=0;
    int minTime=0x3f3f3f3f;
    int time1,time2;
    int ti;
    for(int i=0; i<ROUTES.size(); i++)
    {
        for(int j=ROUTES[i].size()-1; j>0; j--)
        {
            if(j==ROUTES[i].size()-1)
            {
                time1=mp[ROUTES[i][j]][ROUTES[i][j-1]].time;
                time2=0;
            }
            else
            {
                time1=mp[ROUTES[i][j]][ROUTES[i][j-1]].time;
                time2=timeDifference( mp[ROUTES[i][j]][ROUTES[i][j-1]], mp[ROUTES[i][j+1]][ROUTES[i][j]]);
            }
            VOTES_SUM[i]=VOTES_SUM[i]+time1+time2;
        }
        if(VOTES_SUM[i]<minTime)
        {
            minTime=VOTES_SUM[i];
            min1=i;
        }
    }
    cout<<"花费时间最短的为第"<<min1+1<<"条"<<endl;
    printLine(min1);
    cout<<"花费时间为"<<minTime<<"分钟。"<<endl;
}

void minimalLikelihood()//错过换成可能性最小
{
    int min1=0;
    int minTime=0x3f3f3f3f;
    int time2;
    int flag = 0;
    int VIS[ROUTES.size()]= {0}; //标记是否需要换乘
    int v[ROUTES.size()]= {0}; //标记该条路是否符合时间条件
    for(int i=0; i<ROUTES.size(); i++)
    {
        int VOTES_SUM[ROUTES[i].size()] = {1};
        for(int j=ROUTES[i].size()-1; j>0; j--)
        {
            if(j==ROUTES[i].size()-1)
            {
                time2=0;
            }
            else
            {
                time2=timeDifference( mp[ROUTES[i][j]][ROUTES[i][j-1]], mp[ROUTES[i][j+1]][ROUTES[i][j]]);
                VIS[i]=0;
            }
            VOTES_SUM[j]=time2;
        }
        if(VIS[i]==1)
        {
            flag++;
            cout<<"花费时间最短的为第"<<flag+1<<"条"<<endl;
            printLine(i);
            break;
        }
        else
        {
            for(int j=1; j<ROUTES[i].size()-1; j++)
            {
                if(VOTES_SUM[j]<10 || VOTES_SUM[j]>720)
                {
                    v[i]=1;
                }
            }
        }
        if(v[i]!=1)
        {
            flag++;
            cout<<flag<<"、";
            printLine(i);
        }
    }
    if(flag!=0)
    {
        cout<<"以上路线换乘失败可能性小"<<endl;
    }
    else cout<<"所有路径失败换乘可能性很大，请慎重选择";
    return ;
}

void leastFare()//票价最少
{


    int min1 = 0x3f3f3f3f,temp;
    //cout <<ROUTES.size();
    for(int i=0; i<ROUTES.size(); i++)
    {
        double softPrice_all= 0.0;
        for(int j=ROUTES[i].size()-1; j>0; j--)
        {

            softPrice_all+=mp[ROUTES[i][j]][ROUTES[i][j-1]].softSleeperFare;
        }
        if(min1 > softPrice_all)
        {

            min1 = softPrice_all;
            temp = i;
        }

    }
    cout<<"票价最少为第"<<temp+1<<"条"<<endl;
    for(int j  = ROUTES[temp].size()-1; j>=0; j--)
    {
        if(j==0)
            cout << VIS2[ROUTES[temp][j]];
        else cout << VIS2[ROUTES[temp][j]]<<"->";
    }
    cout <<endl;
    for(int j  = ROUTES[temp].size()-1; j>=1; j--)
    {

        timeshow(ROUTES[temp][j],ROUTES[temp][j-1]);
        cout<<endl;
    }

    cout << "总票价(软卧):"<< min1<<"元"<<endl;

}

void selectLine(int num,char x)//选择路线

{


    fstream f;
    f.open("ticketInformation.txt",ios::out|ios::app|ios::in);
    f << R.name<<" "<<ROUTES[num].size()<<" ";
    int VOTES_SUM = 0,time1,time2;
    cout <<"您选择的路线是：\n";
    double softPrice_all = 0.0,hardPrice_all = 0.0,hardSeatPrice_all = 0.0;
    for(int j  = ROUTES[num].size()-1; j>=0; j--)
    {
        f << VIS2[ROUTES[num][j]]<<" ";
        if(j==0)
        {
            cout << VIS2[ROUTES[num][j]];

        }

        else
        {

            cout << VIS2[ROUTES[num][j]]<<"->";

        }
    }
    for(int j  = ROUTES[num].size()-1; j>=1; j--)
    {
        hardPrice_all+=mp[ROUTES[num][j]][ROUTES[num][j-1]].hardSleeperFare;
        softPrice_all+=mp[ROUTES[num][j]][ROUTES[num][j-1]].softSleeperFare;
        hardSeatPrice_all+=mp[ROUTES[num][j]][ROUTES[num][j-1]].hardSeatFare;
    }
    cout <<"\n";
    while(1)
    {
        switch (x)
        {
        case '0':
        {
            printf("票价:%.2f\n",softPrice_all);
            break;
        }
        case '1':
        {

            printf("票价:%.2f\n",hardPrice_all);
            break;
        }
        case '2':
        {

            printf("票价:%.2f\n",hardSeatPrice_all);
            break;
        }
        default:
        {
            cout<<"请选择正确坐席:";
            cin>>x;
            continue;

        }
        }
        break;
    }
    for(int j=ROUTES[num].size()-1; j>0; j--)
    {
        if(j==ROUTES[num].size()-1)
        {
            time1=mp[ROUTES[num][j]][ROUTES[num][j-1]].time;
            time2=0;
        }
        else
        {
            time1=mp[ROUTES[num][j]][ROUTES[num][j-1]].time;
            time2=timeDifference( mp[ROUTES[num][j]][ROUTES[num][j-1]], mp[ROUTES[num][j+1]][ROUTES[num][j]]);
        }
        VOTES_SUM+=time1+time2;
    }
    cout<<"花费时间为"<<VOTES_SUM<<"分钟。"<<endl;
    f<<endl;
    f.close();

}

void ticketingSystem()
{
    cout <<"===========================================欢迎进入购票系统===================================="<<endl;
    while(1)
    {

        while(1)
        {
            cout << "请输入您出发的城市：";
            cin >> BEGIN_NAME ;
            cout <<"请输入您的目的地：";
            cin >> END_NAME;

            if(findName(BEGIN_NAME) && findName(END_NAME))
            {
                break;
            }
            else
            {
                cout<<"您输入的地点有误，请输入正确地点"<<endl;
                continue;
            }
        }


        if( BEGIN_NAME == END_NAME)
        {
            cout << "您所在的城市就是您的目的地，可以选择步行或者开车前往，请输入不同的两个城市:\n";
            continue;
        }
        Route(VIS1[BEGIN_NAME],VIS1[END_NAME]);
        if(ROUTES.size()==0)
        {
            cout << "抱歉，您选择的起点和目的地之间没有路线,请重新输入:\n";
            continue;
        }
        else
        {
            cout << "您可以选的的路径有："<<"\n";
            for(int i = 0; i<ROUTES.size(); i++)
            {
                double softPrice_all = 0.0,hardPrice_all = 0.0,hardSeatPrice_all = 0.0;
                cout << i+1<<"、";
                for(int j  = ROUTES[i].size()-1; j>=0; j--)
                {
                    if(j==0)
                        cout << VIS2[ROUTES[i][j]];
                    else cout << VIS2[ROUTES[i][j]]<<"->";
                }
                cout <<endl;
                for(int j  = ROUTES[i].size()-1; j>=1; j--)
                {
                    hardPrice_all+=mp[ROUTES[i][j]][ROUTES[i][j-1]].hardSleeperFare;
                    softPrice_all+=mp[ROUTES[i][j]][ROUTES[i][j-1]].softSleeperFare;
                    hardSeatPrice_all+=mp[ROUTES[i][j]][ROUTES[i][j-1]].hardSeatFare;
                    timeshow(ROUTES[i][j],ROUTES[i][j-1]);
                    cout << "车厢:"<<"硬卧"<<" "<<"票价:"<<mp[ROUTES[i][j]][ROUTES[i][j-1]].hardSleeperFare<<" "<<"车厢:"<<"软卧"<<" "<<"票价:"<<mp[ROUTES[i][j]][ROUTES[i][j-1]].softSleeperFare<<" "<<"车厢:"<<"硬卧"<<" "<<"票价:"<<mp[ROUTES[i][j]][ROUTES[i][j-1]].hardSeatFare;

                    cout<<endl;

                }
                printf("总票价:硬卧:%.2f 软卧;%.2f 硬座:%.2f\n",hardPrice_all,softPrice_all,hardSeatPrice_all);
                cout << endl;

            }
            break;

        }


    }
    cout << "您可以选择的回家方案有:1、换乘次数最少"<< " "<< "2、耗时最短"<<" "<<"3、票价总和最少(优先选择软卧)" << " "<< "4、错过换成的可能性最小"<<" "<<"0、进入选票系统"<<endl;



    cout<<"请输入你想选择的是: //例如;1\n";

    while(1)
    {
        int flag = 0;
        char i;
        cin >> i;
        switch(i)
        {
        case '1' :
        {
            //求中间节点最少的路
            leastTransfer(VIS1[BEGIN_NAME],VIS1[END_NAME]);
            cout<<"可以继续选择方案或输入0进入选票系统\n"<<endl;
            break;
        }
        case '2':
        {
            lastestTime();
            cout<<"可以继续选择方案或输入0进入选票系统\n"<<endl;
            //求最短路
            break;
        }
        case '3' :
        {
            leastFare();
            cout<<"可以继续选择方案或输入0进入选票系统\n"<<endl;
            //求票价做少
            break;
        }
        case '4' :
        {
            //错过换成可能性最小
            minimalLikelihood();
            cout<<"可以继续选择方案或输入0进入选票系统\n"<<endl;
            break;
        }
        case '0' :
        {
            selectRoute();
            return ;

        }
        default :
        {
            cout << "由于开发人员能力有限，暂时未开发您所需要的查询要求,实在抱歉，请重新输入:\n";
            break;

        }
        }
    }
}

void selectRoute()
{
    cout <<"===========================================欢迎进入选票系统===================================="<<endl;
    int num;
    cout <<"请输入您选择路线的编号：\n";
    while(1)
    {
        cin >> num;
        if(num>ROUTES.size() ||num<=0)
        {
            cout<<"请输入正确的路线编号:"<<endl;
        }
        else
        {
            cout <<"请输入座位类型:(0)软卧,(1)硬卧,(2)硬座:\n";
            char j;
            cin >> j;
            selectLine(num-1,j);
            cout<<"订票已完成，请按任意键返回";
            system("pause");
            break;
        }

    }
}

int main()
{
    while(1)
    {
        system("cls");
        Init();
        edgeInit();
        Initfile();
        Information();
        meau();
        hi();
    }

    return 0;
}
